package cn.qingyun.gis.modules.baogan.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.baogan.entity.CrmStaff;
import cn.qingyun.gis.modules.baogan.service.CrmStaffService;
import cn.qingyun.gis.modules.baogan.vo.request.GridCodeAndKeyListRequest;
import cn.qingyun.gis.modules.baogan.vo.response.StaffAndTeamVo;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: CRM人员表后台管理
 * @Version: V1.0
 */
@Api(tags = "CRM人员")
@RestController
@RequestMapping("/admin/baogan/staff")
@Slf4j
public class CrmStaffController extends BasicController<CrmStaff, CrmStaffService> {

    @Resource
    private CrmStaffService iCrmStaffService;

    /**
     * 分页列表查询
     *
     * @param crmStaff
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "CRM人员表-分页列表查询")
    @ApiOperation(value = "CRM人员表-分页列表查询", notes = "CRM人员表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CrmStaff crmStaff,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CrmStaff> queryWrapper = QueryGenerator.initQueryWrapper(crmStaff, req.getParameterMap());
        Page<CrmStaff> page = new Page<>(pageNo, pageSize);
        IPage<CrmStaff> pageList = iCrmStaffService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param crmStaff
     * @return
     */
    @AutoLog(value = "CRM人员表-添加")
    @ApiOperation(value = "CRM人员表-添加", notes = "CRM人员表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CrmStaff crmStaff) {
        iCrmStaffService.save(crmStaff);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param crmStaff
     * @return
     */
    @AutoLog(value = "CRM人员表-编辑")
    @ApiOperation(value = "CRM人员表-编辑", notes = "CRM人员表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmStaff crmStaff) {
        iCrmStaffService.updateById(crmStaff);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "CRM人员表-通过id删除")
    @ApiOperation(value = "CRM人员表-通过id删除", notes = "CRM人员表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iCrmStaffService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "CRM人员表-批量删除")
    @ApiOperation(value = "CRM人员表-批量删除", notes = "CRM人员表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iCrmStaffService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "CRM人员表-通过id查询")
    @ApiOperation(value = "CRM人员表-通过id查询", notes = "CRM人员表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        CrmStaff crmStaff = iCrmStaffService.getById(id);
        if (crmStaff == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(crmStaff);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param crmStaff
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CrmStaff crmStaff) {
        return super.exportXls(request, crmStaff, CrmStaff.class, "CRM人员表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CrmStaff.class);
    }

    @AutoLog(value = "微网格-看管划分-查询自有人员及团队")
    @ApiOperation(value = "微网格-看管划分-查询自有人员及团队",tags = "sgc")
    @PostMapping("/listStaffAndTeam")
    public Result listStaffAndTeam(@RequestBody @Validated GridCodeAndKeyListRequest request) {
        StaffAndTeamVo result = iCrmStaffService.listStaffAndTeam(request);
        return Result.okData(result);
    }

}
